package com.example.netty.realNetty.packet;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.util.Random;

public class Client {
    public static void main(String[] args) throws InterruptedException {

        extracted();

    }

    private static void extracted() throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    socketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                    socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            char c = '0';
                            ByteBuf byteBuf = ctx.alloc().buffer();
                            Random random = new Random();
                            for (int i = 0; i < 5; i++) {
                                //ByteBuf byteBuf = ctx.alloc().buffer(17);
                                //ByteBuf byteBuf = ctx.alloc().buffer();
                                //byteBuf.writeBytes(new byte[]{'1','2','3','4','5','6','7','8','1','2','3','4','5','6','7','8','a'});
                                //ctx.writeAndFlush(byteBuf);
                                //ctx.close();
                                StringBuilder stringBuilder = stringBuilder(c,random.nextInt(256) + 1);
                                byteBuf.writeBytes(stringBuilder.toString().getBytes());
                                c ++;
                            }
                            ctx.writeAndFlush(byteBuf);
                        }
                    });
                }
            });
            ChannelFuture connect = bootstrap.connect("localhost", 8080);
            ChannelFuture sync = connect.sync();
            Channel channel = sync.channel();
            ChannelFuture future = channel.closeFuture();
            future.sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            group.shutdownGracefully();
        }

    }
    public static StringBuilder stringBuilder(char c,int a){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < a; i++) {
            stringBuilder.append(c);
        }
        stringBuilder.append("\n");
        return stringBuilder;
    }
}
